package com.fzb.forum4j.controller;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fzb.forum4j.po.Admin;
import com.fzb.forum4j.po.Article;
import com.fzb.forum4j.po.Category;
import com.fzb.forum4j.service.api.IArticleService;
import com.fzb.forum4j.service.api.ICategoryService;
import com.fzb.forum4j.service.api.ITagService;
import com.fzb.forum4j.service.api.IUserService;
import com.opensymphony.xwork2.ActionContext;
@Controller("articleAction")
@Scope("prototype")
public class ArticleAction extends CommonsAction<Article> {
 
	/**
	 * 资讯社区管理
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name="articleServiceImpl")
	private IArticleService articleService;
	
	
	
	@Resource(name="tagServiceImpl")
	private ITagService tagService;
	
	@Resource(name="userServiceImpl")
	private IUserService userServiceImpl;
	
	@Resource(name="categoryServiceImpl")
	private ICategoryService categoryService;
	public String details(){
		articleService.getArticleAndComment(getQmd());
		if(ActionContext.getContext().getApplication().get("articleViewCount")!=null)
		{
			if(((Integer)ActionContext.getContext().getApplication().get("articleViewCount"))>50)
			{
				articleService.updateArticlesViews((Map<String,Integer>)ActionContext.getContext().getApplication().get("article"));
				ActionContext.getContext().getApplication().remove("article");
				ActionContext.getContext().getApplication().remove("articleViewCount");
			}
		}
		
		return "details";
	}
	@Override
	public String list() {
		articleService.getList(getQmd());
		return "list";
	}
	@Override
	public String add() {
		getT().setPubTime(new Date());
		userServiceImpl.disposeArticleContext(getQmd());
		getQmd().getT().getCategory().setCategroyId(categoryService.getEidByCatelog(getT().getCategory().getCatalog()));
		if(getT().getCategory().getCatalog().equals("news")){
			if(getRequest().getSession().getAttribute("adminMsg")!=null){
				if(((Admin)getRequest().getSession().getAttribute("adminMsg")).getAdminRight()==1){
					getT().getUser().setUserId(1);
				}
			}
		}
		else{
			getQmd().getData().put("403", "你无法在这个板块发布文章");
			return "403";
		}
		String str=super.add();
		getQmd().getT().setCid(getQmd().getId());
		tagService.addTag(getQmd().getT().getKeywords().split(","), getQmd().getT());
		getQmd().setRows(Integer.parseInt(getWebSite().get("rows")));
		Set<Category> categories=new LinkedHashSet<Category>();
		Category category=new Category();
		category.setCatalog("news");
		categories.add(category);
		category=new Category();
		category.setCatalog("question");
		categories.add(category);
		getQmd().setT(new Article());
		articleService.getInitData(getQmd(),categories);
		ServletActionContext.getServletContext().setAttribute("data", getQmd().getData());
		return str;
	}
	public void validateAdd(){
		clearErrors();
		if(getUserMsg()!=null){
			getT().setUser(getUserMsg());
			getT().setPubTime(new Date());
			getT().setViews(0);
		}
		else{
			setAttrToSession("forward",getRequest().getRequestURL());
		}
	}
	public String search(){
		System.out.println(getQmd().getKeyword());
		articleService.getArticlesByQmd(getQmd());
		return "search";
	}

}
